# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from scrapy_fuligirl.scrapy_fuligirl.items import (AlbumsImgItem, AlbumsItem,
                                                   ModelItem, TagItem)
from scrapy_fuligirl.scrapy_fuligirl.spiders.models import ModelsSpider


class ScrapyFuligirlPipeline:
    def open_spider(self, spider):
        engine = create_engine('mysql://root:520777@localhost:3306/fuligirl')
        ModelsSpider.metadata.create_all(engine)
        self.Session = sessionmaker(bind=engine)

    def process_item(self, item, spider):
        session = self.Session()
        
        if isinstance(item, ModelItem):
            model = ModelItem(url=item['url'], text=item['text'])
        if isinstance(item, TagItem):
            model = TagItem(url=item['url'], text=item['text'])
        if isinstance(item, AlbumsItem):
            model = AlbumsItem(url=item['url'], text=item['text'])
        elif isinstance(item, AlbumsImgItem):
            model = AlbumsImgItem(
                album_url=item['album_url'], 
                title=item['title'],
                time=item['time'],
                href=item['href']
            )
        else:
            raise ValueError("Unhandled item type")
        
        session.add(model)
        session.commit()
        session.close()
        
        return item
